/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import za.org.foobar.famon.util.DB;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class FailureGroup extends ModelObject {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private List<Key> failureIdList = new ArrayList<Key>();
    @NotPersistent
    private List<Failure> failureList = new ArrayList<Failure>();
                                              //TODO: make failure an owned child
//    @OneToMany(mappedBy = "thread")
//    private List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
//
//    @OneToMany(mappedBy = "thread")
//    private List<LoggingEvent> loggingEventList = new ArrayList<LoggingEvent>();

    public FailureGroup() {
    }

    public Key getId() {
        return id;
    }

    public List<Failure> getFailureList() {
        if (failureList == null && failureIdList != null) {
            failureList = new ArrayList<Failure>();
            for (Key failureId : failureIdList) {
                failureList.add(DB.findEx(Failure.class, failureId));
            }
        }
        return failureList;
    }

    public void addFailure(Failure failure) {
        if (failureIdList == null) {
            failureIdList = new ArrayList<Key>();
            failureList = new ArrayList<Failure>();
        } else if (failureList == null) {
            getFailureList();
        }
        failureIdList.add(failure.getId());
        failureList.add(failure);
    }
}
